#include "thread.h"
#include <iostream>
#include <unistd.h>
using namespace std;

ThreadMutex gThreadMutex;

class Printer : public Runnable
{
private:
    int _id;
public:
    Printer() {_id = 0;}
    void set(int id) {_id = id;}
    void run(Thread * thread, void * arg)
    {
        int * cnt = (int *)arg;
        srand(time(NULL));
        for (int i=0; i<*cnt; i++) {
            {
                ScopeLock lock(&gThreadMutex);
                cout << "Printer" << _id << ":\t" << i << endl;
            }
            usleep(rand() % 100000);
        }
    }

private:
    ThreadMutex _mutex;
};

int main(int argc, char *argv[])
{
    int cnt = 10;
    Thread t[10];
    Printer p[10];
    for (int i=0; i<10; i++) {
        p[i].set(i);
        t[i].start(&p[i], (void *)&cnt);
    }
    for (int i=0; i<10; i++) {
        t[i].join();
    }

    return 0;
}

